#include <my_head.h>

// 获取文件行数
int get_line(FILE *fp){
    int line = 0;
    int value = 0;
    while(EOF != (value = fgetc(fp))){
        if('\n' == value)
            line++;
    }
    return line;
}

int main(int argc, const char *argv[]){
    FILE *fp = fopen("hello.txt", "a+");
    if(NULL == fp)
        ERR_LOG("fopen error");
    // 获取行数
    int line = get_line(fp);
    time_t sec = 0;
    time_t old_sec = 0;
    struct tm *p = NULL;
    while(1){
        if(-1 == time(&sec))
            ERR_LOG("time error");
        if(sec != old_sec){
            if(NULL == (p = localtime(&sec)))
                ERR_LOG("localtime error");
            fprintf(fp, "%d.%04d-%02d-%02d %02d:%02d:%02d\n", \
                ++line, p->tm_year+1900, p->tm_mon+1, p->tm_mday,\
                p->tm_hour, p->tm_min, p->tm_sec);
            fflush(fp); // 手动刷新缓冲区
            old_sec = sec;
        }
    }
    fclose(fp);
    return 0;
}